package com.y.mytest.jm.aes;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * AES加密128位CBC模式工具类
 */
public class AESUtils {

    public static final String KEY = "CNjE5hInPKfqSrO5";//密钥key
    public static final String IV = "kouU7yNdeiwIVcAm";//向量iv

    public static final String AK = "s2ip9g3y3bjr5zz7ws6kjgx3ysr82zzw";//AccessKey
    public static final String SK = "uv8zr0uen7aim8m7umcuooqzdv8cbvtf";//SecretKey

    //加密
    public static String encrypt(String content) throws Exception {
        byte[] raw = KEY.getBytes(StandardCharsets.UTF_8);
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//"算法/模式/补码方式"
        IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ips);
        byte[] encrypted = cipher.doFinal(content.getBytes());
        return new BASE64Encoder().encode(encrypted);
    }

    public static byte[] encrypt2(byte[] content) throws Exception {
        byte[] raw = KEY.getBytes(StandardCharsets.UTF_8);
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//"算法/模式/补码方式"
        IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ips);
        return cipher.doFinal(content);
    }

    //解密  
    public static String decrypt(String content) throws NoSuchPaddingException, NoSuchAlgorithmException, IOException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException, InvalidKeyException {
        byte[] raw = KEY.getBytes(StandardCharsets.UTF_8);
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, ips);
        byte[] encrypted1 = new BASE64Decoder().decodeBuffer(content);
        byte[] original = cipher.doFinal(encrypted1);
        return new String(original);
    }

    public static byte[] decrypt2(byte[] content) throws NoSuchPaddingException, NoSuchAlgorithmException, IOException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException, InvalidKeyException {
        byte[] raw = KEY.getBytes(StandardCharsets.UTF_8);
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, ips);
        return cipher.doFinal(content);
    }

    public static void main(String[] args) throws Exception {
        String charname = "utf-8";
        //String content = "123456789";
        byte[] content = new byte[]{120, -100, 51, 52, 50, 54, 49, 53, 51, -73, -80, 4, 0, 9, 30, 1, -34};
        //content = new String(content2);
        System.out.println("原xx:  " + Arrays.toString(content));
        //byte[] content3 = new byte[]{49, 50, 51, 52, 53, 54, 55, 56, 57};
        //System.out.println(Arrays.equals(content2, content3));
        //加密
        byte[] ens2 = AESUtils.encrypt2(content);
        System.out.println("加密后：" + Arrays.toString(ens2));

        String s = new String(ens2, charname);
        System.out.println("加密后s：" + s);
        byte[] bytes = s.getBytes(charname);
        System.out.println("加密后：" + Arrays.toString(bytes));

        //解密
        System.out.println("解密后：" + Arrays.toString(AESUtils.decrypt2(ens2)));
        System.out.println("解密后：" + new String(AESUtils.decrypt2(ens2)));
    }
}
